package com.ailink.concurrent.thread;

/**
 * Created by Administrator on 2018/3/6
 */
public class MyAtomicTest1 implements Runnable {

    private int i = 0;

    private void asc() {
//        synchronized (this) {
//            i = i + 1;
//        }
        i = i + 1;
    }

    @Override
    public void run() {
        for (int j = 0; j < 100; j++){
            asc();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield();
        }
    }

    public static void main(String[] args) {
        MyAtomicTest1 myAtomicTest1 = new MyAtomicTest1();
        Thread thread1 = new Thread(myAtomicTest1, "1");
        Thread thread2 = new Thread(myAtomicTest1, "2");
        Thread thread3 = new Thread(myAtomicTest1, "3");
        thread1.start();
        thread2.start();
        thread3.start();
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(String.valueOf(myAtomicTest1.i));
        }
    }
}
